package com.baize.baizeapp.test.chat

import android.content.Context
import android.content.Intent
import android.net.NetworkInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.Toast
import com.baize.baizeapp.R
import com.baize.baizeapp.utils.SPUtils
import com.farsunset.cim.sdk.android.CIMEventListener
import com.farsunset.cim.sdk.android.CIMListenerManager
import com.farsunset.cim.sdk.android.CIMPushManager
import com.farsunset.cim.sdk.android.constant.CIMConstant
import com.farsunset.cim.sdk.android.model.Message
import com.farsunset.cim.sdk.android.model.ReplyBody
import com.farsunset.cim.sdk.android.model.SentBody

class ChatActivity : AppCompatActivity(),CIMEventListener {
    companion object{
        fun open(context: Context) {
            context.startActivity(Intent(context,ChatActivity::class.java))
        }
    }

    lateinit var et_account:EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat)
        et_account = findViewById(R.id.et_account)
        CIMPushManager.setLoggerEnable(this,true)
        CIMListenerManager.registerMessageListener(this)
    }

    override fun onResume() {
        super.onResume()
        //恢复接收消息
        CIMPushManager.resume(this)
    }

    //登录
    fun chatLogin(v: View) {
        val account = et_account.text.toString()
        if (TextUtils.isEmpty(account)) {
            Toast.makeText(this,"账号不能为空",Toast.LENGTH_SHORT).show()
        }
        SPUtils.putString("account", account)
        CIMPushManager.connect(this, "192.168.1.212", 8080)
        CIMPushManager.bind(this,account)
    }

    //链接状态
    fun chatConnectState(v: View) {
        Toast.makeText(this,"连接状态：${CIMPushManager.isConnected(this)}",Toast.LENGTH_SHORT).show()
    }

    //登出
    fun chatLogout(v: View) {
        SPUtils.putString("account","")
        CIMPushManager.stop(this)
    }

    //发送消息
    fun chatSendMsg(v: View) {
        val body = SentBody()
        body.setKey("client_cycle_location")
        body.put("uid", "10001")
        body.put("latitude", "123.513514")
        body.put("longitude", "326.151456")
        body.put("location","中国北京市朝阳区**路**号")
        CIMPushManager.sendRequest(this, body)
    }

    override fun onDestroy() {
        super.onDestroy()
        //完全销毁，调用resume不能恢复
        CIMListenerManager.removeMessageListener(this)
        CIMPushManager.destroy(this)
    }

    //消息监听事件
    override fun onMessageReceived(message: Message) {
        Log.i("yanze_chat", "onMessageReceived: 收到服务端推送过来的消息:")
        Log.i("yanze_chat", "$message")
    }

    override fun onReplyReceived(replyBody: ReplyBody) {
        Log.i("yanze_chat", "onReplyReceived: 当调用CIMPushManager.sendRequest()向服务端发送请求，获得相应:")
        Log.i("yanze_chat", "$replyBody")
        /*
         *用户id绑定成功，可以接收消息了，需要判断code 是否是200
         */
        if (replyBody.key.equals(CIMConstant.RequestKey.CLIENT_BIND)) {
            Log.i("yanze_chat", "用户id绑定成功，可以接收消息了。。。")
        }
    }

    override fun onSendFinished(body: SentBody?) {
        Log.i("yanze_chat", "onSendFinished: 调用CIMPushManager.sendRequest()向服务端发送请求成功:")
        Log.i("yanze_chat", "$body")
    }

    override fun onNetworkChanged(info: NetworkInfo?) {
        Log.i("yanze_chat", "onNetworkChanged: 当手机网络发生变化:")
        Log.i("yanze_chat", "$info")
    }

    /**
     * @param hasAutoBind
     *  true 已经自动绑定账号到服务器了，不需要再手动调用bindAccount
     */
    override fun onConnectFinished(hasAutoBind: Boolean) {
        Log.i("yanze_chat", "onConnectFinished: 连接服务器成功:")
        Log.i("yanze_chat", "$hasAutoBind")
    }

    override fun onConnectionClosed() {
        Log.i("yanze_chat", "onConnectionClosed: 断开服务器连接了...")
    }

    override fun onConnectFailed() {
        Log.i("yanze_chat", "onConnectFailed: 接服务器失败...")
    }

    /**
     * @return 排序 值越大优先级越高
     */
    override fun getEventDispatchOrder(): Int {
        Log.i("yanze_chat", "getEventDispatchOrder: 监听器在容器里面的排序。值越大则越先接收...")
        return 100
    }

}